package com.cellulargames.view;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.cellulargames.control.Controller;
import com.cellulargames.control.Painter;
import com.cellulargames.listener.ICanvas;
import com.cellulargames.model.Font59;
import com.sun.pisces.Paint;

public class VideoPoker implements ICanvas{

	public static Image coins[];
	
	private NumberPainter credito;
	
	private NumberPainter saldo;
	
	private NumberPainter apuesta;
	
	private Image maquina;
	
	private Image holdOff;
	
	private Image holdOn;
	
	private Image holdDisabled;
	
	public static Image cardback;
	
	public static CardPainter cards;
	
	private Image buttons[];
	
	private Image disableButtons[];
	
	private Image yesnoButtons[];
	
	private Image winImage;
	
	private String messages[] = {"¡¡Ganaste!!", "Bienvenido, utilice las direccionales para moverse en el escenario.", "Perdiste..."};
	
	private int press1;
	
	private int press2;
	
	private int press3;
	
	private int currentButton;
	
	private Font59 text;
	
	private Font59 don;
	
	public static boolean reloadCards;
	
	public static HandsPainter handsPainter;
	
	public static int holdButton;
	
	public static int waitCounter = 100;
	
	public static boolean doble;
	
	public static boolean winDoble;
	
	public static boolean allow;
	
	public VideoPoker()throws Exception{
		
		loadImages();
		
		if( Controller.apuesta > 0 ){
			currentButton = 0;
		}
		else if( Controller.saldo > 0 ){
			currentButton = 1;
		}
		else if( Controller.credito > 0 ){
			currentButton = 2;
		}
		
	}
	
	private void loadImages() throws Exception{
		cards = new CardPainter(37, 60);
		maquina = Image.createImage("/maquina.png");
		cardback = Image.createImage("/cardback.png");
		buttons = new Image[3];
		buttons[0] = Image.createImage("/bjugar.png");
		buttons[1] = Image.createImage("/apostar.png");
		buttons[2] = Image.createImage("/cobrar.png");		
		disableButtons = new Image[3];
		disableButtons[0] = Image.createImage("/unbjugar.png");
		disableButtons[1] = Image.createImage("/unapostar.png");
		disableButtons[2] = Image.createImage("/uncobrar.png");
		yesnoButtons = new Image[3];
		yesnoButtons[0] = Image.createImage("/si.png");
		yesnoButtons[1] = Image.createImage("/no.png");
		credito = new NumberPainter("/numbers2.png", NumberPainter.HORIZONTAL);
		saldo = new NumberPainter("/numbers2.png", NumberPainter.HORIZONTAL);
		apuesta = new NumberPainter("/numbers2.png", NumberPainter.HORIZONTAL);
		coins = new Image[2];		
		coins[0] = Image.createImage("/coins.png");
		coins[1] = Image.createImage("/coins2.png");
		text = new Font59("/font.fnt");
		don = new Font59("/font.fnt");
		holdOff = Image.createImage("/h1.png");
		holdOn = Image.createImage("/h2.png");
		holdDisabled = Image.createImage("/AH1N1.png");
		handsPainter = new HandsPainter();
		winImage = Image.createImage("/win2.png");
	}

	public void keyPressed(int key) {
		
		if( waitCounter < 50 ){
			return;
		}
		
		switch ( Painter.getInstance().getGameAction(key) ) {
		
		case Canvas.UP:
			if( step > 0 || doble ){
				holdButton = (doble) ? 1 : 0;
				handsPainter.senalar(holdButton);
			}
			break;
			
		case Canvas.DOWN:
			if( step >= 0 ){
				if( !doble ){
					handsPainter.unSenala();
				}
			}
			break;
		
		case Canvas.RIGHT:
			
			if( handsPainter.isSenalando() ){
				holdButton = ( holdButton < 4 ) ? holdButton + 1 : holdButton;
				handsPainter.senalar(holdButton);
				break;
			}
			
			if( currentWin > 0 ){
				currentButton = 2;
				return;
			}
			
			if( currentButton+1 == 1 && Controller.saldo > 0 && step == 0 ){
				currentButton++;
			}
			else if( currentButton+1 == 2 && Controller.credito > 0 ){
				currentButton++;
			}
			
			break;
			
		case Canvas.LEFT:
			
			if( handsPainter.isSenalando() ){
				if( doble ){
					holdButton = ( holdButton > 1 ) ? holdButton-1 : holdButton;
				}
				else{
					holdButton = ( holdButton > 0 ) ? holdButton-1 : holdButton;	
				}				
				handsPainter.senalar(holdButton);
				break;
			}			

			if( currentWin > 0 ){
				currentButton = 0;
				return;
			}
			
			if( currentButton-1 == 1 && Controller.saldo > 0 ){
				currentButton--;
			}
			else if( currentButton-1 == 2 && Controller.credito > 0 ){
				currentButton--;
			}
			else if( currentButton-1 == 0 && Controller.apuesta > 0 ){
				currentButton = 0;
			}
			
			break;
		
		case Canvas.FIRE:
			
			if( currentWin > 0 ){
				
				handsPainter.pressed();
				
				if( handsPainter.isSenalando() ){
					
					Controller.holdCard(holdButton);
					
					if( Controller.currentCartas[holdButton].valor >= Controller.currentCartas[0].valor ){
						winDoble = true;
						currentWin = currentWin*2;						
						waitCounter = 0;
						allow = true;
					}
					else{
						waitCounter = 0;
						step = 2;						
						winDoble = false;
						doble = false;
						currentWin = 0;						
					}
					
					handsPainter.unSenala();					
					break;
					
				}	
				
				press1 = (currentButton == 0) ? 2 : 0;	
				press3 = (currentButton == 2) ? 2 : 0;
				
				if( currentButton == 2 ){
					doble = false;
					winDoble = false;
					Controller.credito += currentWin;
					currentWin = 0;									
				}
				else{
					handsPainter.senalar(holdButton);
					allow = false;
					doble = true;
					Controller.reset();
					Controller.dealCards();
					reloadCards = true;
				}
				
				return;
			}
			
			if( handsPainter.isSenalando() ){
				Controller.holdCard(holdButton);
				handsPainter.pressed();
				return;
			}
			
			press1 = (currentButton == 0) ? 2 : 0;
			press2 = (currentButton == 1) ? 2 : 0;
			press3 = (currentButton == 2) ? 2 : 0;
			
			handsPainter.pressed();
			
			if( currentButton == 1 && Controller.saldo > 0 ){
				CoinDialog.getInstance().show();
			}			
			else if( currentButton == 0 && Controller.apuesta > 0 ){
				if( step == 0 || step == 1 ){
					Controller.dealCards();
					if( step == 1 ){
						waitCounter = 0;
						currentWin = Controller.getPay();
						if( currentWin > 0 ){
							allow = true;
						}
					}
					step++;
					reloadCards = true;			
				}
			}
			
			break;

		default:
			break;
		
		}
		
		Painter.getInstance().repaint();
		
	}
	
	private int step;
	
	private int indexText = 1;
	
	public static int currentWin;

	public void keyReleased(int key) {
		press1 = press2 = press3 = 0;
		if( Painter.getInstance().getGameAction(key) == Canvas.FIRE ){
			handsPainter.released();
		}
		Painter.getInstance().repaint();
	}

	private int xText = Painter.getInstance().getWidth();  
	
	public void paint(Graphics g) {
		
		g.drawImage(maquina, 0, 0, Graphics.LEFT|Graphics.TOP);
		
		g.setColor(0x333333);
		g.fillRect(0, 0, Painter.getInstance().getWidth(), 21);
		
		credito.setNumber(Controller.credito);
		credito.setLocation(coins[0].getWidth()+4, (22-credito.getHeightNumber())/2);
				
		saldo.setNumber(Controller.saldo);
		saldo.setLocation((Painter.getInstance().getWidth()-saldo.getWidthNumber())/2, (22-saldo.getHeightNumber())/2);
				
		apuesta.setNumber(Controller.apuesta);
		apuesta.setLocation(Painter.getInstance().getWidth()-apuesta.getWidthNumber()-2, (22-apuesta.getHeightNumber())/2);		
		
		g.drawImage(coins[0], 2, (22-coins[0].getHeight())/2, Graphics.LEFT|Graphics.TOP);
		g.drawImage(coins[1], Painter.getInstance().getWidth()-apuesta.getWidthNumber()-coins[1].getWidth()-4, 
				(22-coins[1].getHeight())/2, Graphics.LEFT|Graphics.TOP);
		
		credito.paint(g);
		saldo.paint(g);
		apuesta.paint(g);
		
		g.setClip(0, 0, Painter.getInstance().getWidth(), Painter.getInstance().getHeight());
		
		int x = (Painter.getInstance().getWidth()-((cards.width*5)+8))/2;
		int y = 162;
		
		if( currentWin > 0 && waitCounter > 25 && !doble ){
			g.setColor(0x000000);
			g.fillRect(10, y, 220, 62);
			don.PrintString(g, (Painter.getInstance().getWidth()-don.stringWidth("¿ DOBLE O NADA ?"))/2, y+(62-don.getFontHeight())/2, "¿ DOBLE O NADA ?", Font59.HCENTER, Font59.TOP);			
		}		
		else if( Controller.currentCartas[0] != null && step > 0 ){
			cards.paint(g, x, y);
			if( currentWin == 0 ){
				for(int i=0;i<5;i++){
					if( Controller.currentCartas[i].hold ){
						g.drawImage(holdOn, x+(cards.width-holdOn.getWidth())/2, y+cards.height+5, Graphics.LEFT|Graphics.TOP);
					}
					else{
						g.drawImage(holdOff, x+(cards.width-holdOff.getWidth())/2, y+cards.height+5, Graphics.LEFT|Graphics.TOP);
					}
					x+=cards.width+4;
				}
			}
		}
		
		if( doble ){
			cards.paint(g, x, y);
		}
		
		if( step == 0 || currentWin > 0 ){
			x = (Painter.getInstance().getWidth()-((cards.width*5)+8))/2;
			for( int i=0;i<5;i++ ){
				if( currentWin == 0 ){
					g.drawImage(cardback, x, y, Graphics.LEFT|Graphics.TOP);
				}
				g.drawImage(holdDisabled, x+(cards.width-holdDisabled.getWidth())/2, y+cards.height+5, Graphics.LEFT|Graphics.TOP);
				x+=cards.width+2;
			}
		}
		
		g.setClip(0, 0, Painter.getInstance().getWidth(), Painter.getInstance().getHeight());
		
		y = 271;
		
		x = 8;
		
		if( Controller.apuesta > 0 && currentWin == 0 ){
			g.drawImage(buttons[0], x, y+press1, Graphics.LEFT|Graphics.TOP);
		}
		else if( currentWin == 0 || ( currentWin > 0 && waitCounter < 25 ) ){
			g.drawImage(disableButtons[0], x, y+press1, Graphics.LEFT|Graphics.TOP);
		}
		else if( allow  ){			
			g.drawImage(yesnoButtons[0], 
					(Painter.getInstance().getWidth()-(yesnoButtons[0].getWidth()*2))/2, y+press1, Graphics.LEFT|Graphics.TOP);
		}
		
		x = 83;
		
		if( Controller.saldo > 0 && step == 0 && currentWin == 0 ){
			g.drawImage(buttons[1], x, y+press2, Graphics.LEFT|Graphics.TOP);
		}
		else if( currentWin == 0 || ( currentWin > 0 && waitCounter < 25 ) ){
			g.drawImage(disableButtons[1], x, y+press2, Graphics.LEFT|Graphics.TOP);
		}
		
		x = 157;
		
		if( Controller.credito > 0 && step == 0  && currentWin == 0 ){
			g.drawImage(buttons[2], x, y+press3, Graphics.LEFT|Graphics.TOP);
		}
		else if( currentWin == 0 || ( currentWin > 0 && waitCounter < 25 ) ){
			g.drawImage(disableButtons[2], x, y+press3, Graphics.LEFT|Graphics.TOP);
		}
		else if( currentWin > 0 && waitCounter > 25 && allow ){
			g.drawImage(yesnoButtons[1], 
					((Painter.getInstance().getWidth()-(yesnoButtons[0].getWidth()*2))/2)+yesnoButtons[0].getWidth()+2, y+press3, Graphics.LEFT|Graphics.TOP);
		}
		
		g.setClip(0, 0, Painter.getInstance().getWidth(), Painter.getInstance().getHeight());
		handsPainter.paint(g, currentButton);
		
		waitCounter++;
		
		if( waitCounter == 25 && winDoble ){			
			Controller.reset();
			Controller.dealCards();
			reloadCards = true;
			doble = false;			
		}
		else if( waitCounter >= 50 && step == 2 && !winDoble ){
			Controller.reset();
			step = 0;
		}
		
		if( waitCounter < 25 && currentWin > 0 ){
			g.drawImage(winImage, (Painter.getInstance().getWidth()-winImage.getWidth())/2, 
					(Painter.getInstance().getHeight()-winImage.getHeight())/2, Graphics.LEFT|Graphics.TOP);
			indexText = 0;
			xText = Painter.getInstance().getWidth();	
		}
		else{
			indexText = 1;
		}
		
		y = 136;
		
		g.setClip(9, 131, 222, 23);
		text.PrintString(g, xText, 136, messages[indexText], Font59.LEFT, Font59.TOP);		
		xText-=2;
		if( xText < -(text.stringWidth(messages[indexText])+Painter.getInstance().getWidth()*2) ){
			xText = Painter.getInstance().getWidth();
		}
		
	}
		
}


